package com.example.address_list;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class DataAbility extends Ability {
    RdbStore rdbStore;
    DatabaseHelper databaseHelper;
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "DataAbility onStart");
        databaseHelper=new DatabaseHelper(this);
        StoreConfig storeConfig=StoreConfig.newDefaultConfig("name.db");
        RdbOpenCallback callback=new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                rdbStore.executeSql("create table student(id int primary key  auto_increment not null,name varchar(10) not null,age int not null)");
            }
            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {

            }
        };
        rdbStore=databaseHelper.getRdbStore(storeConfig,1,callback);
    }
    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates= DataAbilityUtils.createRdbPredicates(predicates,"student");
        ResultSet resultSet=rdbStore.query(rdbPredicates,columns);
        return resultSet;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        int i=-1;
        String path=uri.getLastPath();
        if ("student".equalsIgnoreCase(path)){
            i= (int) rdbStore.insert("student",value);
        }
        return i;
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates= DataAbilityUtils.createRdbPredicates(predicates,"student");
        int i = rdbStore.delete(rdbPredicates);
        return i;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates= DataAbilityUtils.createRdbPredicates(predicates,"student");
        int i = rdbStore.update(value, rdbPredicates);
        return i;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}